package com.cat.cc.taglibrary.view;

import android.content.Context;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * 可拖拽的view
 */
public class DragBox extends RelativeLayout {

    static interface OnTapUpListener {
        void up(float xMoved, float yMoved);
    }

    private int myWidth;
    private int myHeight;
    //父控件的宽高
    private int parentWidth;
    private int parentHeight;
    private OnTapUpListener mOnTapUpListener;


    public void setTempRectF(final RectF aTempRectF) {
        this.boundRectF = aTempRectF;
    }


    private RectF boundRectF;

    public void setOnTapUpListener(final OnTapUpListener aOnTapUpListener) {
        this.mOnTapUpListener = aOnTapUpListener;
    }

    //是否拖动
    private boolean isDrag = false;

    public void setCanMove(final boolean aCanMove) {
        this.canMove = aCanMove;
    }

    private boolean canMove = true;

    private float xMoved = 0f;
    private float yMoved = 0f;

    /**
     * 父控件
     */
    private ViewGroup parentView;

    public boolean isDrag() {
        return isDrag;
    }

    public DragBox(Context context) {
        super(context);
    }

    public DragBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        myWidth = getMeasuredWidth();
        myHeight = getMeasuredHeight();
        if (parentHeight == 0 || parentWidth == 0) {
            parentView = (ViewGroup) getParent();
            parentView.post(new Runnable() {
                @Override
                public void run() {
                    parentWidth = parentView.getMeasuredWidth();
                    parentHeight = parentView.getMeasuredHeight();
                }
            });
        }
    }

    private float downX;
    private float downY;

    private int lastLeft;
    private int lastTop;

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    this.xMoved = 0f;
                    this.yMoved = 0f;
                    isDrag = false;
                    downX = event.getX();
                    downY = event.getY();
                    lastLeft = getLeft();
                    lastTop = getTop();

                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!canMove) {
                        break;
                    }

                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    int l, r, t, b;
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) {
                        this.xMoved += xDistance;
                        this.yMoved += yDistance;
                        isDrag = true;
                        //在父控件中左边的位置 = 当前在父控件中的位置 - 内边距 + 拖动的距离
                        l = (int) (getLeft() + xDistance);
                        //在父控件中的右边位置 = 在左边的位置 + 自己的宽-

                        r = l + myWidth;
                        t = (int) (getTop() + yDistance);
                        b = t + myHeight;
                        if (boundRectF == null) break;
                        int INTER = 0;
                        if (getX() < INTER||getX()<boundRectF.left-INTER) {
                            if (xDistance < 0) {
                                break;
                            }
                        }
                        if (getX() + myWidth > boundRectF.right||getX() + myWidth>parentWidth) {
                            if (xDistance > 0) {
                                break;
                            }
                        }
                        if (getY() < INTER||getY()<boundRectF.top-INTER) {
                            if (yDistance < 0) {
                                break;
                            }
                        }
                        if (getY() + myHeight > boundRectF.bottom ||getY() + myHeight >parentHeight) {
                            if (yDistance > 0) {
                                break;
                            }
                        }
                        this.layout(l, t, r, b);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (!canMove) {
                        break;
                    }
                    if (mOnTapUpListener != null) {
                        mOnTapUpListener.up(getLeft() - lastLeft, getTop() - lastTop);
                    }
                    if (isDrag) {
                        //如果是拖动，就拦截点击事件
                        return false;
                    }
                    break;
            }
            return super.onTouchEvent(event);
        }
        return false;
    }

}